<?php
/**
 * User: lsx
 * Date: 2017/7/13
 * Time: 9:30
 * Progress:0%
 */

namespace library\common\drawing;


use library\common\arr;
use library\common\str;
use library\date;

class ImageSynthetise
{
    /**
     * resize
     * @author LSX
     * @param string $image_bg
     * @param string $image_merge
     * @param int $dst_x
     * @param int $dst_y
     * @param int $pct
     * @param string $base_url
     * @return array
     */
    public static function image_merge_to_image($image_bg,$image_merge,$dst_x,$dst_y,$pct,$base_url){
        $bigImgPath = $image_bg;
        $qCodePath =$image_merge;
        $filename='';
        $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
        $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
        list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
        imagecopymerge($bigImg, $qCodeImg, $dst_x, $dst_y, 0, 0, $qCodeWidth, $qCodeHight, $pct);
        list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);

        switch ($bigType) {
            case 1: //gif
                header('Content-Type:image/gif');
                $postfix='gif';
                $filename=self::create_path($base_url,$postfix);
                $result=imagegif($bigImg,$filename);
                break;
            case 2: //jpg
                header('Content-Type:image/jpg');
                $postfix='jpg';
                $filename=self::create_path($base_url,$postfix);
                $result=imagejpeg($bigImg,$filename);
                break;
            case 3: //png
                header('Content-Type:image/png');
                $postfix='png';
                $filename=self::create_path($base_url,$postfix);
                $result=imagepng($bigImg,$filename);
                break;
            default:
                # code...
                break;
        }
        imagedestroy($bigImg);
        imagedestroy($qCodeImg);
        return array(
            'success'=>true,
            'filename'=>$filename
        );

    }


    public static function string_merge_to_image($image_bg,$content,$base_url){
        $filename='';
        $bigImgPath = $image_bg;
        $bigImg= imagecreatefromstring(file_get_contents($bigImgPath));

        $font = './public/wechat/static/font/'.'DroidSansChinese.ttf';//字体
        $black = imagecolorallocate($bigImg, 255, 146, 66);//字体颜色 RGB

        $fontSize = 16;   //字体大小
        $circleSize = 0; //旋转角度
        $left = 180;      //左边距
        $top = 60;       //顶边距

        imagefttext($bigImg, $fontSize, $circleSize, $left, $top, $black, $font, $content);

        list($bgWidth, $bgHight, $bgType) = getimagesize($bigImgPath);
        switch ($bgType) {
            case 1: //gif
                header('Content-Type:image/gif');
                $postfix='gif';
                $filename=self::create_path($base_url,$postfix);
                $result=imagegif($bigImg,$filename);
                break;
            case 2: //jpg
                header('Content-Type:image/jpg');
                $postfix='jpg';
                $filename=self::create_path($base_url,$postfix);
                $result=imagejpeg($bigImg,$filename);
                break;
            case 3: //jpg
                header('Content-Type:image/png');
                $postfix='png';
                $filename=self::create_path($base_url,$postfix);
                $result=imagepng($bigImg,$filename);
                break;
            default:
                break;
        }
        imagedestroy($bigImg);
        return array(
            'success'=>true,
            'filename'=>$filename
        );
    }

    public static function create_path($basel_url,$postfix){
        $store_path=date('Y',time()).'/'.date('m',time()).'/'.date('d',time()).'/';
        $filepath=$basel_url.$store_path;
        if(!is_dir($filepath)){
            mkdir($filepath,0777,true);
        }
        $filename=$filepath.str::randomStr(32).'.'.$postfix;
        return $filename;
    }


    public static function image_png_size_add($imgsrc,$base_url){
        list($width,$height,$type)=getimagesize($imgsrc);
        $new_width = 80;
        $new_height =80;
        $filename='';

        switch($type){
            case 2:
                header('Content-Type:image/jpeg');
                $image_wp=imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefromjpeg($imgsrc);
                $re=imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                $postfix='png';
                $filename=self::create_path($base_url,$postfix);
                imagejpeg($image_wp, $filename,75);
                imagedestroy($image_wp);
                break;
            case 3:
                header('Content-Type:image/png');
                $image_wp=imagecreatetruecolor($new_width, $new_height);
                $image = imagecreatefrompng($imgsrc);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                $postfix='png';
                $filename=self::create_path($base_url,$postfix);
                imagejpeg($image_wp, $filename,75);
                imagedestroy($image_wp);
                break;
        }
        return array(
            'success'=>true,
            'filename'=>$filename
        );
    }


}